package cn.dapeng.interview;

import java.util.Stack;

/**
 * 使用两个栈实现队列
 * <p>
 * 栈：先进后出
 * 队列：先进先出
 */
public class TwoStackQueue_T01 {

    private int count = 0;
    private final Stack<Integer> pushStack = new Stack<>();

    private final Stack<Integer> popStack = new Stack<>();

    public void push(Integer t) {
        pushStack.push(t);
        count ++;
    }

    public Integer pop() {

        if (!popStack.isEmpty()) {
            count --;
            return popStack.pop();
        }

        while (!pushStack.isEmpty()) {
            popStack.push(pushStack.pop());
        }
        count --;
        return popStack.pop();
    }


    public boolean isEmpty() {
        return count == 0;
    }

    public static void main(String[] args) {
        TwoStackQueue_T01 queue = new TwoStackQueue_T01();
        for (int i = 0; i < 5; i++) {
            queue.push(i);
        }

        System.out.println("value :: " + queue.pop());
        System.out.println("value :: " + queue.pop());

        for (int i = 5; i < 10; i++) {
            queue.push(i);
        }

        while (!queue.isEmpty()) {
            System.out.println("value :: " + queue.pop());
        }
    }
}
